﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_UserEdit : System.Web.UI.Page
{
    protected int id = Helper.getQuery("userID");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UserEntities ue = ub.getUser(id);
            if (id > 0)
            {
                btnSubmit.Text = "Sửa";
                bindDropDownList1();
                txtUserName.Text = ue._userName;

                txtEmail.Text = ue._email;
                txtTenThe.Text = ue._creaitCard;
                txtSoThe.Text = ue._cardNumber;
                txtFullName.Text = ue._fullName;
                txtAddress.Text = ue._address;
                txtBirthday.Text = ue._birthday;
                txtPhoneNumber.Text = ue._phoneNumber.ToString();

            }
            else
                btnSubmit.Text = "Thêm";
        }
    }
    
    UserBusiness ub = new UserBusiness();
    public void bindDropDownList1()
    {
        RoleBusiness rb = new RoleBusiness();
        DropDownList1.DataSource = rb.getAllRole();
        DropDownList1.DataTextField = "roleName";
        DropDownList1.DataValueField = "roleID";
        DropDownList1.DataBind();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (btnSubmit.Text == "Thêm")
        {
            if (ub.checkUser(txtUserName.Text))
            {
               
                    string[] value = { txtUserName.Text,Helper.EncodePass(txtPass.Text),txtFullName.Text,txtBirthday.Text,rdoFemale.Checked?"0":"1",txtEmail.Text,
                                 txtAddress.Text,txtPhoneNumber.Text,txtTenThe.Text,txtSoThe.Text,"0",DropDownList1.SelectedValue.ToString()};
                    ub.Insert(value);
               
            }
        }
        else
        {

            string[] value = { id.ToString(),txtUserName.Text,Helper.EncodePass(txtPass.Text),txtFullName.Text,txtBirthday.Text,rdoFemale.Checked?"0":"1",txtEmail.Text,
                                 txtAddress.Text,txtPhoneNumber.Text,txtTenThe.Text,txtSoThe.Text,"0",DropDownList1.SelectedValue.ToString()};
                ub.Edit(value);
            
        }
        Response.Redirect("User.aspx");
    }
    
}